package DOM;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadClassificationXMLForQuestioner {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// DOM - is org.w3c.dom.* - classes
		
		String xmlFileName=ReadClassificationXMLForQuestioner.class.getResource("../resources/classification.xml").getPath();
		
		
		File fXmlFile =new File(xmlFileName);
		DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder;
		try {
			dBuilder = dbFactory.newDocumentBuilder();
			Document doc=dBuilder.parse(fXmlFile);
			
			doc.getDocumentElement().normalize();
			
			System.out.println("Root element :"+doc.getDocumentElement().getNodeName());
			
			NodeList classes=doc.getElementsByTagName("class");
			
			
			for(int i=0;i<classes.getLength();i++){
				Node nNode=classes.item(i);
				
				if(nNode.getNodeType()==Node.ELEMENT_NODE){
					Element eElement=(Element)nNode;
					System.out.println("class id="+eElement.getAttribute("id"));
					System.out.println(eElement.getChildNodes().getLength());
						
						NodeList categories=eElement.getChildNodes();
						for(int j=0;j<categories.getLength();j++){
							Node ncategorie=categories.item(j);
							
							if(ncategorie.getNodeType()==Node.ELEMENT_NODE){
								Element eCategorie=(Element)ncategorie;
								System.out.println("- "+eCategorie.getAttribute("name"));
							}
						}
						
						

				}
				
				
			}
			
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		System.out.println("===============================");
		System.out.println("\t\t"+"a");
	}
	
	public static void nTabulations(int n){
		for(int i=0;i<n;i++){
			System.out.print("\t");
		}
	}

}
